package io.fredia.femicro.admin.rpc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.fredia.femicro.admin.rpc.service.PermissionService;
import io.fredia.femicro.api.vo.authority.PermissionInfo;
import io.fredia.femicro.api.vo.user.UserInfo;
import io.fredia.femicro.cache.annotation.Cache;

@RestController
@RequestMapping("api")
public class UserRest {
	@Autowired
	private PermissionService permissionService;

	@Cache(key = "permission")
	@RequestMapping(value = "/permissions", method = RequestMethod.GET)
	public @ResponseBody List<PermissionInfo> getAllPermission() {
		return permissionService.getAllPermission();
	}

	@Cache(key = "permission:u{1}")
	@RequestMapping(value = "/user/un/{username}/permissions", method = RequestMethod.GET)
	public @ResponseBody List<PermissionInfo> getPermissionByUsername(@PathVariable("username") String username) {
		return permissionService.getPermissionByUsername(username);
	}

	@RequestMapping(value = "/user/validate", method = RequestMethod.POST)
	public @ResponseBody UserInfo validate(String username, String password) {
		return permissionService.validate(username, password);
	}

}
